package com.sfac.javaSpringBoot.modules.test.service.impl;

import com.sfac.javaSpringBoot.modules.common.entity.Result;
import com.sfac.javaSpringBoot.modules.test.dao.StudentDao;
import com.sfac.javaSpringBoot.modules.test.entity.Student;
import com.sfac.javaSpringBoot.modules.test.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Description StudentServiceImpl
 * @Author HymanHu
 * @Date 2021/9/14 14:58
 */
@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentDao studentDao;

    @Override
    @Transactional
    public Result<Student> insertStudent(Student student) {
        studentDao.saveAndFlush(student);
        return new Result<>(Result.ResultCode.SUCCESS.code,
                "Insert success.", student);
    }

    @Override
    @Transactional
    public Result<Student> updateStudent(Student student) {
        studentDao.saveAndFlush(student);
        return new Result<>(Result.ResultCode.SUCCESS.code,
                "Update success.", student);
    }

    @Override
    @Transactional
    public Result<Object> deleteStudentByStudentId(int studentId) {
        studentDao.deleteById(studentId);
        return new Result<>(Result.ResultCode.SUCCESS.code,
                "Delete success.");
    }

    @Override
    public Student getStudentByStudentId(int studentId) {
        return studentDao.findById(studentId).orElseGet(null);
    }
}
